package register.students;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Driver;
/**
 * Servlet implementation class EmailStrore
 */
public class EmailStore extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmailStore() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String usremail = request.getParameter("email");
		String fname = request.getParameter("fullname");
		String pwd = request.getParameter("pwd");
		String sex = request.getParameter("sex");
		String stid = request.getParameter("studentid");
		Integer stuid = Integer.valueOf(stid); 
		//Date dt = null;
		java.util.Date dt = new java.sql.Date(0);
		//java.sql.Date dt = new java.util.Date();
		java.sql.Date dts = new java.sql.Date(0);
		SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/yyyy");
		try {
			dt =sdf.parse(request.getParameter("birthday"));
			//dts = (java.sql.Date) dt;
			//# java.util.Date fromDate = df.parse(fromdate1);  
			dts = new java.sql.Date(dt.getTime()); 
		} catch (ParseException e) {
			System.out.println(e.toString());
			e.printStackTrace();
		}
		boolean bemail= true; //validate(usremail);
		
		if(bemail)
		{
			System.out.println("before ading the email");
			addEmail(stuid, usremail, fname, pwd, sex, dts);
			response.sendRedirect("./home.html");
		}
		else
			response.sendRedirect("./index.html");
	}

	private void addEmail(Integer stuid, String usremail, String fname, String pwd, String sex, Date dt) {
		// TODO Auto-generated method stub

		String query = "INSERT INTO users (studentid, useremail, fullname, password, sex, birthday) values('"
			+stuid+"','"+usremail+"','"+fname+"','"+pwd+"','"+sex+"','"+dt+"')";
		System.out.println(query);
	    String connectionString = "jdbc:mysql://localhost:3306/yooim";
	    String userName = "root";
	    String password = "mapat";
	    Connection conn; 
	    ResultSet rs = null;
	    try {
	    	
	    	Class.forName("com.mysql.jdbc.Driver").newInstance();
	    	conn = DriverManager.getConnection(connectionString,userName,password);
	    	Statement st = conn.createStatement();
	    	st.execute(query);
	    	conn.close();
	    }
	    catch(Exception e)
	    {
	    	System.out.println("Error while connection to DB - addemail : "+e.toString());
	    }
	    
	}

	public boolean validate(String usremail) {

	    String query = "Select * from users where useremail='"+ usremail.toLowerCase() +"'";
	    String connectionString = "jdbc:mysql://localhost:3306/yooim";
	    String userName = "root";
	    String password = "mapat";
	    Connection conn = null; 
	    ResultSet rs = null;
	    try {
	    	
	    	Class.forName("com.mysql.jdbc.Driver").newInstance();
	    	conn = DriverManager.getConnection(connectionString,userName,password);
	    	Statement st = conn.createStatement();
	    	rs = st.executeQuery(query);
	    	System.out.println(rs.getFetchSize());
	    	//conn.close();
	    }
	    catch(Exception e)
	    {
	    	System.out.println("Error while connection to DB - validate : "+e.toString());
	    }
	    
	    if(rs != null)
	    {
	    	try {
				if(rs.first())
				{
					System.out.println("user with "+rs.getString(1)+rs.getString(2)+rs.getString(3)+" does exist");
					conn.close();
					return false;
				}
					else {
						System.out.println("no record");
						conn.close();
						return true;
					}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("error SQL"+e.toString());
				//conn.close();
				return true;
			}
	    }
	    else return true;
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
